package daoIMP;
import bean.Student;
import dao.StudentDAO;
import java.sql.*;
import java.util.*;

import connection.DataBaseConnection;

public class StudentDAOIMP implements StudentDAO{
	// 添加操作
	    public void insert(Student s){
	      String sql = "INSERT INTO student (id, name) values (?,?)";
	    PreparedStatement pstmt = null;
	    DataBaseConnection conn = null;
	    //针对数据库的具体操作
	    try{
	        conn = new DataBaseConnection();
	        
	        pstmt = conn.getConnection().prepareStatement(sql);
	        pstmt.setLong(1,s.getID());
	        //pstmt.setString(1,s.getID());
	        pstmt.setString(2,s.getName());
	  
	        pstmt.executeUpdate();
	        pstmt.close();
	        conn.close();
	        }
	     catch(Exception e){  }
	      }

	    public void update(Student s){   
	    	String sql = "UPDATE student set name='"+s.getName()+"' WHERE id="+s.getID();
		    PreparedStatement pstmt1 = null;
		    DataBaseConnection conn1 = null;
		    //针对数据库的具体操作
		    try{
		        conn1 = new DataBaseConnection();
		        
		        pstmt1 = conn1.getConnection().prepareStatement(sql);
//		        pstmt1.setLong(1,s.getID());//显示更新后的数据
//		        pstmt1.setString(2,s.getName());
		  
		        pstmt1.executeUpdate(sql);
		        pstmt1.close();
		        conn1.close();
		        }
		     catch(Exception e){  }
	    }

        public void delete(String iD){
        	String sql = "DELETE student WHERE id=" + iD + "'";
    	    PreparedStatement pstmt = null;
    	    DataBaseConnection conn = null;
    	    //针对数据库的具体操作
    	    try{
    	        conn = new DataBaseConnection();
    	        conn.getConnection();
    	        pstmt.executeUpdate(sql);
    	        pstmt.close();
    	        conn.close();
    	        }
    	     catch(Exception e){  }
        }
	    
        
 	    public List<Student> findAll(){
 	    	String sql ="SELECT * FROM student";
 	    	PreparedStatement pstmt = null;
 	    	DataBaseConnection conn = null;
 	    	List<Student> users = new ArrayList();
 	    //针对数据库的具体操作
 	    	try{
 		        conn = new DataBaseConnection();
 		        
 		        pstmt = conn.getConnection().prepareStatement(sql);
 		        
 		        //显示更新后的数据
 		        ResultSet rs = pstmt.executeQuery(sql);
 	            while (rs.next()) {
 	            	
 	            	Student user=new Student();
 	                user.setID(rs.getLong(2));
 	                user.setName(rs.getString(1));
 	                users.add(user);
 	            }
 		        
 		        pstmt.close();
 		        conn.close();
 		        }
 		     catch(Exception e){  }
 	    	
 	    	return users;
 		    }
	    
	    public Student findByID(long iD){
	    	String sql ="SELECT * FROM student where id = "+iD;
	    	PreparedStatement pstmt = null;
		    DataBaseConnection conn = null;
		    Student s=new Student();
		    //针对数据库的具体操作
		    try{
		        conn = new DataBaseConnection();
		        
		        pstmt = conn.getConnection().prepareStatement(sql);
		        
		        ResultSet rs = pstmt.executeQuery(sql);
		        
//		        pstmt.setLong(1,s.getID());//显示更新后的数据
//		        pstmt.setString(2,s.getName());
//		        s.setID(pstmt.getString(1));
		        if(rs.next())
		        {
		        	s.setID(rs.getLong(2));
		        	s.setName(rs.getString(1));
		        }
		        pstmt.executeUpdate();
		        pstmt.close();
		        conn.close();
		        }
		     catch(Exception e){  }
		    return s;
		    }
	    
	    }
